package com.thread.homework;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

/**
 * @author げっしょく
 * @date 2021/8/6
 */
public class Test6ex implements Runnable {
    private int[] arr = {85, 100, 60, 10, 50, 20, 22, 8};
    private int i = -1;
    private ArrayList<Integer> list = new ArrayList<>();
    private boolean ye = true;

    @Override
    public void run() {
        synchronized (arr) {
            if (ye) {
                Random r = new Random();
                for (int j = 0; j < arr.length; j++) {
                    int at = r.nextInt(arr.length);
                    if (!list.contains(at)) {
                        list.add(at);
                    } else {
                        boolean b = true;
                        while (b) {
                            at = r.nextInt(arr.length);
                            if (!list.contains(at)) {
                                list.add(at);
                                b = false;
                            }
                        }
                    }
                }
            }
            ye = false;
        }
        int count = 0;
        while (true) {
            synchronized (this) {
                i++;
                if (i < arr.length) {
                    System.out.println(Thread.currentThread().getName() + "抽中了" + arr[list.get(i)]);
                    count += arr[list.get(i)];
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    break;
                }
            }
        }
        System.out.println(Thread.currentThread().

                getName() + "总共抽中了：" + count);
    }
}

